<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="shortcut icon" href="../img/eraser-32.png"/>
    <link rel="bookmark" href="../img/eraser-32.png" type="image/x-icon"/>
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>

</head>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="项目名称/编码" v-model="pagination.queryString" style="width: 200px;"></el-input>
                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新增</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="code" label="编码" align="center"></el-table-column>
                <el-table-column prop="name" label="项目名称" align="center"></el-table-column>
                <el-table-column label="运动强度" align="center">
                    <template slot-scope="scope">
                        <span>{{scope.row.sportIntensity == '1' ? '中' : scope.row.sportIntensity == 2 ? '强' : '弱'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="kilostepEquivalent" label="千步当量" align="center"></el-table-column>
                <el-table-column prop="sportTime" label="运动时间(单位:分钟)" align="center"></el-table-column>
                <el-table-column prop="minAge" label="最小年龄" align="center"></el-table-column>
                <el-table-column prop="maxAge" label="最大年龄" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="updateCreat(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增运动项目" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" label-position="right" :model="formData" :rules="rules"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="编码" prop="code">
                                                <el-input v-model="formData.code"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="运动强度">
                                                <el-select v-model="formData.sportIntensity">
                                                    <el-option label="中" value="1"></el-option>
                                                    <el-option label="强" value="2"></el-option>
                                                    <el-option label="弱" value="0"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>

                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="项目名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="千步当量" prop="kilostepEquivalent">
                                                <el-input v-model="formData.kilostepEquivalent"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="运动时间" prop="sportTime">
                                                <el-input v-model="formData.sportTime"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="最小年龄" prop="minAge">
                                                <el-input v-model="formData.minAge"></el-input>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="最大年龄" prop="maxAge">
                                                <el-input v-model="formData.maxAge"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" label-position="right" :model="formData" :rules="rules"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="编码" prop="code">
                                                <el-input v-model="formData.code"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="运动强度">
                                                <el-select v-model="formData.sportIntensity">
                                                    <el-option label="中" value="1"></el-option>
                                                    <el-option label="强" value="2"></el-option>
                                                    <el-option label="弱" value="0"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>

                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="项目名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="千步当量" prop="kilostepEquivalent">
                                                <el-input v-model="formData.kilostepEquivalent"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="运动时间" prop="sportTime">
                                                <el-input v-model="formData.sportTime"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="最小年龄" prop="minAge">
                                                <el-input v-model="formData.minAge"></el-input>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="最大年龄" prop="maxAge">
                                                <el-input v-model="formData.maxAge"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="update()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<!--引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../plugins/healthmobile.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            tableData: [],
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//添加表单时候可见
            rules: {//校验规则
                name: [{required: true, message: '运动项目为必填项', trigger: 'blur'}],
                sportTime: [{required: true, message: '运动时间为必选项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },

        methods: {
            //分页查询
            findPage() {
                //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/sport/findPage.do", param).then((resp) => {
                    this.pagination.total = resp.data.total;
                    this.dataList = resp.data.rows;
                })
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible = true;
                //默认切换到第一个标签(基本信息)
                this.activeName = 'first';
            },
            //添加
            handleAdd() {
                axios.post("/sport/add.do", this.formData).then((resp) => {
                    //关闭新增窗口
                    this.dialogFormVisible = false;
                    if (resp.data.flag) {
                        this.$message({
                            message: resp.data.message,
                            type: 'success'
                        });
                    } else {
                        this.$message.error(resp.data.message);
                    }
                }).finally(() => {
                    this.findPage();
                })
            },
            //弹出编辑窗口
            updateCreat(row) {
                this.resetForm();
                this.dialogFormVisible4Edit = true;
                //为了绑定角色需要的对象,将user传入到formdata里
                axios.get("/sport/findById.do?id=" + row.id).then((resp) => {
                    if (resp.data.flag) {
                        this.formData = resp.data.data;
                    } else {
                        this.$message.error("查询失败")
                    }
                });
            },
            //添加角色
            update() {
                axios.post("/sport/update.do", this.formData).then((resp) => {
                    this.dialogFormVisible4Edit = false;
                    if (resp.data.flag) {
                        this.$message({
                            message: resp.data.message,
                            type: 'success'
                        });
                    } else {
                        this.$message.error("添加失败")
                    }
                }).finally(() => {
                    this.findPage();
                })
            },

            // 重置表单
            resetForm() {
                this.formData = {};//重置数据
            },

            //切换页码
            handleCurrentChange(currentPage) {
                //currentPage为切换后的页码
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            handleDelete(row) {
                this.$confirm("确定要删除吗?", "提示", {type: 'warning'}).then((res) => {
                    axios.get("/sport/deletebyid.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                            this.findPage();
                        } else {
                            this.$message.error(res.data.message);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        message: '已取消',
                        type: 'info'
                    });
                });
            },
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            }
        }

    })
</script>
</html>